package com.app.community.controller;

import com.app.community.exception.ErrorCodeEnum;
import com.app.community.model.entity.User;
import com.app.community.service.AvatarService;
import com.app.community.service.HomeService;
import com.wordnik.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 个人主页
 * @Date 2020/10/21 13:47
 **/
@Slf4j
@Controller
public class HomeController {
    @Autowired
    AvatarService avatarService;

    @Autowired
    HttpSession httpSession;

    @Autowired
    HomeService homeService;


    @GetMapping("home")
    public String getHome(Model model) {
        // 根据用户的id进行home定位
        String userId = (String) httpSession.getAttribute("userId");
        // 指定一个信号进行拦截，不允许未登录查看
        if (userId == null || userId.isEmpty() ){
            model.addAttribute("signal",1);
            model.addAttribute("error", ErrorCodeEnum.HAVENOTLOGIN.getDescription());
        }

        // 添加用户头像
        model.addAttribute("avatar",avatarService.selectUserAvatar(userId));

        User user = homeService.searchUserInfo(userId);
        // 避免空指针异常
        if (user == null){
            user = new User();
        }
        // 将user对象传递给前端
        model.addAttribute("user", user);
        return "common/home";
    }


    @ApiOperation("上传用户背景")
    @PostMapping("homeCoverUpLoad")
    public String homeCoverUpLoad( @RequestParam(value = "coverPic", required = false) MultipartFile coverPic,Model model) {

        String userId = (String)httpSession.getAttribute("userId");

        if (coverPic != null && !coverPic.isEmpty()) {
            homeService.updatecoverPic(coverPic, userId);
        }

        User user = homeService.searchUserInfo(userId);
        // 避免空指针异常
        if (user == null){
            user = new User();
        }
        // 将user对象传递给前端
        model.addAttribute("user", user);

        return "common/home::coverPicFragment";
    }

    @ApiOperation("上传用户头像")
    @PostMapping("homeAvatarUpLoad")
    public String homeAvatarUpLoad(@RequestParam(value = "avatar", required = false) MultipartFile avatar,Model model) {

        String userId = (String)httpSession.getAttribute("userId");

        if (avatar != null && !avatar.isEmpty()) {
            log.info("上传头像");
            homeService.updateAvatar(avatar, userId);
        }

        User user = homeService.searchUserInfo(userId);
        // 避免空指针异常
        if (user == null){
            user = new User();
        }
        // 将user对象传递给前端
        model.addAttribute("user", user);

        return "common/home::avatarFragment";
    }


    @PostMapping("home")
    public String uploadHomeInfo(
            @RequestParam(value = "nickName", required = false) String nickNmae,
            @RequestParam(value = "introduction", required = false) String introduction,
            @RequestParam(value = "sex", required = false) String sex,
            @RequestParam(value = "subject", required = false) String subject,
            @RequestParam(value = "year", required = false) String year) {

        String userId = (String)httpSession.getAttribute("userId");

        // 逐一进行非空，非null判断
        if (nickNmae != null && !nickNmae.isEmpty()){
            homeService.updateNickName(nickNmae,userId);
        }

        // 允许删除introduction
        if (introduction != null){
            homeService.updateIntro(introduction,userId);
        }

        if (sex != null && !sex.isEmpty()){
            homeService.updateSex(sex,userId);
        }

        if (subject != null && !subject.isEmpty()){
            homeService.updateSubject(subject,userId);
        }

        if (year != null && !year.isEmpty()){
            homeService.updateYear(year,userId);
        }

        return "redirect:home";
    }

}
